package com.xiaohan.turtlesoup.config;

import com.volcengine.ark.runtime.service.ArkService;
import lombok.Data;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @program: TurtleSoup
 * @description: AI调用配置类
 * @author: 韩小豪
 * @create: 2025-03-22 13:48
 **/

@Configuration
@ConfigurationProperties(prefix = "ai")  //读取的配置文件内容前缀
@Data
public class AiConfig {

	private String apiKey;

	/**
	 * 创建用以调用ai服务的客户端bean对象【arkService】
	 * 参考火山方舟中的api调用文档
	 * 固定写法 + 改造
	 */
	@Bean
	public ArkService arkService() {
		// 从环境变量中获取您的 API Key。此为默认方式，您可根据需要进行修改
		// 此为默认路径，您可根据业务所在地域进行配置
		String baseUrl = "https://ark.cn-beijing.volces.com/api/v3";

		ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS);

		Dispatcher dispatcher = new Dispatcher();
//		dispatcher.setMaxRequests(100);        // 全局最大并发数
//		dispatcher.setMaxRequestsPerHost(20);   // 每个主机的最大并发数

//		ArkService service = ArkService.builder().dispatcher(dispatcher).connectionPool(connectionPool).baseUrl(baseUrl).apiKey(apiKey).build();

		return ArkService.builder().dispatcher(dispatcher).connectionPool(connectionPool).baseUrl(baseUrl).apiKey(apiKey).build();
	}
}
